﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using myWindowsMediaPlayer.Model;
using System.Windows.Data;
using System.Windows.Input;

namespace myWindowsMediaPlayer.ViewModel
{
    class masterMusicViewModel : viewModelBase
    {
        private databaseMusic db;
        private ObservableCollection<musicViewModel> musics;
        private ICollectionView collectionView;
        private ICommand addInCurrentList;
        private masterReadChoiceViewModel choiceList;

        public masterMusicViewModel(masterReadChoiceViewModel choiceList)
        {
            this.db = new databaseMusic();
            this.musics = new ObservableCollection<musicViewModel>();

            foreach (musicModel music in db.Musics)
                this.musics.Add(new musicViewModel(music));
            this.collectionView = CollectionViewSource.GetDefaultView(this.musics);
            this.choiceList = choiceList;
        }

        public ICommand AddInCurrentList
        {
            get
            {
                if (this.addInCurrentList == null)
                    addInCurrentList = new relayCommand(() => this.AddInList());
                return addInCurrentList;
            }
        }

        public void AddInList()
        {
            if (choiceList.Medias.Contains(new libraryViewModel(this.collectionView.CurrentItem as musicViewModel)) != true)
            {
                if (choiceList.Medias.Count == 0)
                    choiceList.MediaUri = new Uri((this.collectionView.CurrentItem as musicViewModel).Path);
                choiceList.Medias.Add(new libraryViewModel(this.collectionView.CurrentItem as musicViewModel));
            }
        }

        public ObservableCollection<musicViewModel> Musics
        {
            get { return this.musics; }
        }

        public musicViewModel SelectedMusic
        {
            get { return this.collectionView.CurrentItem as musicViewModel; }
        }
    }
}
